/*
 * @Description: 数据同步进度条服务
 * @Author: duan_shuangjun 丶
 * @Date: 2025-01-27 12:00:00
 * @LastEditTime: 2025-01-27 12:00:00
 */
import { createApp, h } from 'vue'
import DataSyncProgress from './index.vue'

interface SyncProgressOptions {
    visible?: boolean
    totalSteps?: number
    currentStep?: number
    statusText?: string
}

interface SyncProgressInstance {
    setProgress: (step: number, total: number, status: string) => void
    close: () => void
}

class DataSyncProgressService {
    private _instance_: any = null
    private _parent_: Element | null = null
    private _app_: any = null
    private _container_: Element | null = null

    /**
     * 显示数据同步进度条
     * @param {SyncProgressOptions} options 进度条配置
     * @param {Element | string} el 挂载位置，默认挂载在body
     */
    show(options: SyncProgressOptions = {}, el?: Element | string): Promise<SyncProgressInstance> {
        return new Promise((resolve) => {
            // 如果已经存在实例，先关闭
            if (this._instance_) {
                this.close()
            }

            // 创建容器
            this._container_ = document.createElement('div')

            // 保存this引用
            const self = this

            // 创建Vue应用
            this._app_ = createApp({
                render() {
                    return h(DataSyncProgress, {
                        ...options,
                        visible: true,
                        onClose: () => {
                            // 不允许用户手动关闭
                        }
                    })
                }
            })

            // 挂载应用
            this._app_.mount(this._container_)

            // 确定挂载位置
            if (!el) {
                this._parent_ = document.body
            } else if (this.isHTMLElement(el)) {
                this._parent_ = el as Element
            } else {
                this._parent_ = document.querySelector(el as string)
            }

            if (this._parent_) {
                this._parent_.appendChild(this._container_)
            }

            // 立即返回组件实例
            const progressInstance: SyncProgressInstance = {
                setProgress: (step: number, total: number, status: string) => {
                    // 通过全局变量获取组件实例
                    if (window.__dataSyncProgressInstance__) {
                        window.__dataSyncProgressInstance__.setProgress(step, total, status)
                    }
                },
                close: () => self.close()
            }

            this._instance_ = progressInstance
            resolve(progressInstance)
        })
    }

    /**
     * 关闭进度条
     */
    close() {
        if (this._container_ && this._parent_) {
            this._parent_.removeChild(this._container_)
        }
        if (this._app_) {
            this._app_.unmount()
        }
        this._instance_ = null
        this._app_ = null
        this._container_ = null
    }

    /**
     * 更新进度
     * @param {number} step 当前步骤
     * @param {number} total 总步骤
     * @param {string} status 状态文本
     */
    updateProgress(step: number, total: number, status: string) {
        if (this._instance_) {
            this._instance_.setProgress(step, total, status)
        }
    }

    private isHTMLElement(obj: any): boolean {
        return obj && obj.nodeType && obj.nodeType === 1
    }
}

// 声明全局变量
declare global {
    interface Window {
        __dataSyncProgressInstance__?: any
    }
}

export default new DataSyncProgressService()
